package com.anhry.app.safety.manage.enterprise.struts.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.djpd.service.DjpdService;
import com.anhry.app.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.app.safety.manage.enterprise.bean.TbCorpType;
import com.anhry.app.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Page;
import com.anhry.app.safety.util.Util;

/**
 * @date:2012-9-17
 * @remark:
 * @author:dongbin
 */

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class CorpConfigAction extends EnterpriseBaseAction{

	private Logger logger = Logger.getLogger(CorpConfigAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String countyId;//
	private String unitId;//
	private String deptId;//
	private String maxTypeId;//
	private String minTypeId;//
	private String corpStatus;//
	
	@Resource
	private DjpdService djpdService;
	/**
	 * 企业列表
	 * @return
	 */
	public String corpList(){
		int rowCount = 0;
		int num = (this.num != null && !"".equals(this.num ))?Integer.parseInt(this.num ):1;
		int size = (this.size != null && !"".equals(this.size ))?Integer.parseInt(this.size ):10;
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String orderHql = " order by t.countyId,t.unitId,t.deptId,t.id";//排序hql语句
			String hql = this.getQuaryHql();
			List objSize = this.enterprisService.getCorpList(hql,null,0,0);
			if(objSize != null && objSize.size() > 0){
				rowCount = Integer.parseInt(objSize.get(0).toString());
			}
			Page page = new Page(size,num,rowCount);
			this.setPage(page);
			this.setCorpList(this.enterprisService.getCorpList(hql,orderHql,page.getStartRow(),page.getSize()));
			
			this.setSdList(this.enterprisService.getSdList((List)this.contextPvd.getServletContextAttr(Util.QU_DEPT_LIST),user));
			this.setTypeList(this.enterprisService.getTypeList(user));
			this.setHyList((List)this.contextPvd.getServletContextAttr(Util.HY_DEPT_LIST));
			if("1".equals(this.forward)){//企业分类管理
				this.setResult("corpClass_list");
			}else if("2".equals(this.forward)){//企业属地调整
				this.setResult("corpSd_list");
			}else if("3".equals(this.forward)){//企业高级查询
				this.setResult("corpGj_list");
			}else if("4".equals(this.forward)){//企业变更信息
				this.setResult("corpBg_list");
			}else if("5".equals(this.forward)){//添加二级企业时，选择一级企业
				this.setResult("corpTwo_corplist");
			}else{//默认跳转到错误页面
				this.setTip("页面错误！");
				return ERROR;
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpConfigAction:list() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 企业类型列表
	 * @return
	 */
	public String corpTypeList(){
		try {
			if(null!=this.corpId && this.corpId.length()>0){
				TbCorpInfo corp = (TbCorpInfo)this.utilService.getObjectById(TbCorpInfo.class, Long.parseLong(this.corpId));
				if(null!=corp){
					this.setCorp(corp);
					this.setTypeList((List)this.contextPvd.getServletContextAttr(Util.MAX_CORP_TYPE));
					this.setResult("corpClass_typelist");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpConfigAction:corpTypeList() 异常："+e.getMessage());
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	
	/**
	 * 设置企业类型
	 * @return
	 */
	public String corpTypeSet(){
		try {
			TbCorpType maxType = null;
			TbCorpType minType = null;
			if(null!=this.getMaxTypeId() && this.getMaxTypeId().length()>0){
				maxType = (TbCorpType)this.utilService.getObjectById(TbCorpType.class, Long.parseLong(this.getMaxTypeId()));
			}
			if(null!=this.getMinTypeId() && this.getMinTypeId().length()>0){
				minType = (TbCorpType)this.utilService.getObjectById(TbCorpType.class, Long.parseLong(this.getMinTypeId()));
			}
			if(null!=this.corpId && this.corpId.length()>0){
				TbCorpInfo corp = (TbCorpInfo)this.utilService.getObjectById(TbCorpInfo.class, Long.parseLong(this.corpId));
				if(null!=corp && !corp.getTbCorpTypeMin().getId().equals(minType.getId())){
					logger.info("Change corp [" + corp.getCorpName() + "]'s type from " + corp.getTbCorpTypeMin().getId() + " to type "
							+ minType.getTypeName());
					corp.setTbCorpType(maxType);//企业类型大类
					corp.setTbCorpTypeMin(minType);//企业类型小类
					
					//该企业以前所有的评级信息全部删除，要把企业的级别，对应的评级信息id和评级次数全部重置
					if(corp.getDjpdId() != null && corp.getDjpdId() != 0L) {
						djpdService.deleteApplyWs1(corp.getDjpdId().toString());
						djpdService.deleteApplyWs2(corp.getDjpdId().toString());
						djpdService.deleteApplyItem(corp.getId().toString());
						djpdService.deleteApply(corp.getId().toString());
						corp.setCorpGrade("");
						corp.setDjpdId(null);
						corp.setDjpdFpDate(null);
						corp.setDjpdNum(0);
					}
					
					this.utilService.update(corp);
					this.setTip("修改成功！");
					
				} else {
					this.setTip("企业类型未修改！");
				}
			}
			
			return this.corpTypeList();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpConfigAction:corpTypeSet() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 属地列表
	 * @return
	 */
	public String corpSdList(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			if(null!=this.corpId && this.corpId.length()>0){
				TbCorpInfo corp = (TbCorpInfo)this.utilService.getObjectById(TbCorpInfo.class, Long.parseLong(this.corpId));
				if(null!=corp){
					this.setCorp(corp);
					this.setSdList(this.enterprisService.getSdList((List)this.contextPvd.getServletContextAttr(Util.QU_DEPT_LIST),user));
					this.setResult("corpSd_deptlist");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpConfigAction:corpTypeList() 异常："+e.getMessage());
		}
		this.setTip(Util.NOTIP);
		return ERROR;
	}
	
	/**
	 * 保存属地
	 * @return
	 */
	public String corpSdSet(){
		try {
			TbUserDept county = null;
			TbUserDept unit = null;
			TbUserDept dept = null;
			if(null!=this.getCountyId() && this.getCountyId().length()>0){
				county = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.getCountyId()));
			}
			if(null!=this.getUnitId() && this.getUnitId().length()>0){
				unit = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.getUnitId()));
			}
			if(null!=this.getDeptId() && this.getDeptId().length()>0){
				dept = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(this.getDeptId()));
			}
			if(null!=this.corpId && this.corpId.length()>0){
				TbCorpInfo corp = (TbCorpInfo)this.utilService.getObjectById(TbCorpInfo.class, Long.parseLong(this.corpId));
				if(null!=corp){
					corp.setTbCounty(county);//县(市、区)、开发区
					corp.setTbStreet(unit);//乡镇
					corp.setTbVillage(dept);//村社区
					this.utilService.update(corp);
					this.setTip("修改成功！");
					return this.corpSdList();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpConfigAction:corpSdSet() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 关闭企业恢复
	 * @return
	 */
	public String corpReset(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			if(null!=this.corpId && this.corpId.length()>0){
				TbCorpInfo corp = (TbCorpInfo)this.utilService.getObjectById(TbCorpInfo.class, Long.parseLong(this.corpId));
				if(null!=corp){
					corp.setUpdateFalg("2");
					corp.setUpdateTime(new Date());
					corp.setUpdateUser(user.getUserName());//
					corp.setCorpStatus("运营");
					corp.setIsDele("0");
					corp.setCorpClose("");
					corp.setCorpCloseWhy("");
					this.utilService.update(corp);
					this.setTip("操作成功！");
					return this.corpList();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpConfigAction:corpSdSet() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 高级查询条件页面
	 * @return
	 */
	public String corpGjSearch(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			this.setKeyWord(this.contextPvd.decode(this.keyWord));
			this.setCorpAdds(this.contextPvd.decode(this.corpAdds));
			this.setSdList(this.enterprisService.getSdList((List)this.contextPvd.getServletContextAttr(Util.QU_DEPT_LIST),user));
			this.setTypeList((List)this.contextPvd.getServletContextAttr(Util.MAX_CORP_TYPE));
			this.setHyList((List)this.contextPvd.getServletContextAttr(Util.HY_DEPT_LIST));
			this.setResult("corpGj_search");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpConfigAction:corpGjSearch() 异常："+e.getMessage());
		}
		return ERROR;
	}
	
	public String getQuaryHql(){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		TbUserDept unit = null;//用户单位
		TbUserDept dept = null;//用户部门
		if(null!=user.getTbUserUnit()){
			unit = user.getTbUserUnit();
		}
		if(null!=user.getTbUserDept()){
			dept = user.getTbUserDept();
		}
		StringBuilder urls = new StringBuilder("");
		StringBuilder hql = new StringBuilder(" where 1=1 ");
		String key = "";
		String key1 = "";
		if(null!=this.cLixu && this.cLixu.length()>0){
			if(null!=this.getSearch() && "1".equals(this.getSearch())){
				this.cLixu = this.cLixu.trim();
			}else{
				this.cLixu = this.contextPvd.decode(this.getCLixu());	
			}
			hql.append(" and t.corpLixu like '%"+this.cLixu+"%'");
			urls.append("cLixu="+this.contextPvd.encode(this.cLixu));
		}else{
			urls.append("cLixu=");
		}
		if(null!=this.cStatus && this.cStatus.length()>0){
			if(null!=this.getSearch() && "1".equals(this.getSearch())){
				this.cStatus = this.cStatus.trim();
			}else{
				this.cStatus = this.contextPvd.decode(this.getCStatus());	
			}
			hql.append(" and t.corpStatus like '%"+this.cStatus+"%'");
			urls.append("&cStatus="+this.contextPvd.encode(this.cStatus));
		}else{
			urls.append("&cStatus=");
		}
		if(null!=this.keyWord && this.keyWord.length()>0){
			if(null!=this.getSearch() && "1".equals(this.getSearch())){
				key = this.getKeyWord().trim();
			}else{
				key = this.contextPvd.decode(this.getKeyWord());	
			}
			this.setKeyWord(key);
			hql.append(" and t.corpName like '%"+key+"%'");
			urls.append("&keyWord="+this.contextPvd.encode(this.keyWord));
		}else{
			urls.append("&keyWord=");
		}
		if(null!=this.corpAdds && this.corpAdds.length()>0){
			if(null!=this.getSearch() && "1".equals(this.getSearch())){
				key1 = this.corpAdds.trim();
			}else{
				key1 = this.contextPvd.decode(this.corpAdds);	
			}
			this.setCorpAdds(key1);
			hql.append(" and t.corpRegAddress like '%"+key+"%'");
			urls.append("&corpAdds="+this.contextPvd.encode(this.corpAdds));
		}else{
			urls.append("&corpAdds=");
		}
		if(this.corpStatus != null && corpStatus.length() > 0) {
			hql.append(" and t.corpStatus = '"+corpStatus+"'");
			urls.append("&corpStatus="+this.contextPvd.encode(this.corpStatus));
		}
		
		if("3".equals(grade) && "3".equals(dept.getDeptClass())){//市科室行业部门
			hql.append(" and t.deptMainId="+dept.getId());
			urls.append("&cHy="+dept.getId().toString());
			this.setCHy(dept.getId().toString());
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else if("4".equals(grade) && "3".equals(dept.getDeptClass())){//县区科室行业部门
			if((null==this.cMaxType || "".equals(this.cMaxType)) && (null==this.cMinType || "".equals(this.cMinType))){
				hql.append(" and t.quDeptIds like '%"+dept.getId()+"%'");
			}else{
				if(null!=this.cMaxType && this.cMaxType.length()>0){
					hql.append(" and t.maxTypeId="+this.cMaxType);
					urls.append("&cMaxType="+this.cMaxType);
				}
				if(null!=this.cMinType && this.cMinType.length()>0){
					hql.append(" and t.minTypeId="+this.cMinType);
					urls.append("&cMinType="+this.cMinType);
				}
			}
			if(null!=this.cHy && this.cHy.length()>0){
				hql.append(" and t.deptMainId="+this.cHy);
				urls.append("&cHy="+this.cHy);
			}
		}else if("5".equals(grade)){//市行业
			if("1".equals(unit.getDeptClass())){//行业监管
				hql.append(" and t.deptMainId="+unit.getId());
				urls.append("&cHy="+unit.getId().toString());
				this.setCHy(unit.getId().toString());
			}else{//专业监管
				if("5310".equals(unit.getId().toString())){//质监局
					hql.append(" and t.corpIsTeshe=1");
				}else if("5311".equals(unit.getId().toString())){//消防局
					
				}else if("5312".equals(unit.getId().toString())){//气象局
					hql.append(" and t.corpIsBlzh=1");
				}
				if(null!=this.cHy && this.cHy.length()>0){
					hql.append(" and t.deptMainId="+this.cHy);
					urls.append("&cHy="+this.cHy);
				}
			}
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else if("6".equals(grade)){//区行业
			if("1".equals(dept.getDeptClass())){
				hql.append(" and t.deptMainId="+dept.getDeptSj());
				urls.append("&cHy="+dept.getDeptSj().toString());
				this.setCHy(dept.getDeptSj().toString());
			}else{
				if("5310".equals(dept.getDeptSj().toString())){//质监局
					hql.append(" and t.corpIsTeshe=1");
				}else if("5311".equals(dept.getDeptSj().toString())){//消防局
					
				}else if("5312".equals(dept.getDeptSj().toString())){//气象局
					hql.append(" and t.corpIsBlzh=1");
				}
				if(null!=this.cHy && this.cHy.length()>0){
					hql.append(" and t.deptMainId="+this.cHy);
					urls.append("&cHy="+this.cHy);
				}
			}
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else{//其他用户
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
			if(null!=this.cHy && this.cHy.length()>0){
				hql.append(" and t.deptMainId="+this.cHy);
				urls.append("&cHy="+this.cHy);
			}
		}
		
		if("4".equals(grade) || "6".equals(grade) || "8".equals(grade)){//县区安委会、区科室、区行业
			hql.append(" and t.countyId="+unit.getId());
			this.setCCounty(unit.getId().toString());
			if(null!=this.cUnit && this.cUnit.length()>0){
				hql.append(" and t.unitId="+this.cUnit);
				urls.append("&cUnit="+this.cUnit);
			}
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}else if("7".equals(grade)){//属地
			hql.append(" and t.countyId="+unit.getId()+" and t.unitId="+dept.getId());
			this.setCCounty(unit.getId().toString());
			this.setCUnit(dept.getId().toString());
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}else{
			if(null!=this.cCounty && this.cCounty.length()>0){
				hql.append(" and t.countyId="+this.cCounty);
				urls.append("&cCounty="+this.cCounty);
			}
			if(null!=this.cUnit && this.cUnit.length()>0){
				hql.append(" and t.unitId="+this.cUnit);
				urls.append("&cUnit="+this.cUnit);
			}
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}
		if(null!=this.cZcType && this.cZcType.length()>0){
			hql.append(" and t.corpReportType="+this.cZcType);
			urls.append("&cZcType="+this.cZcType);
		}
		if(null!=this.cGrade && this.cGrade.length()>0){
			if("E".equals(this.cGrade)){
				hql.append(" and t.corpGrade is null");
			}else{
				hql.append(" and t.corpGrade='"+this.cGrade+"'");
			}
			urls.append("&cGrade="+this.cGrade);
		}
		if(null!=cBtime && cBtime.length()>0){
			hql.append(" and to_char(t.corpRegDate,'yyyy-mm-dd') >= '"+cBtime+"'");
			urls.append("&cBtime="+this.cBtime);
		}
		if(null!=cEtime && cEtime.length()>0){
			hql.append(" and to_char(t.corpRegDate,'yyyy-mm-dd') <= '"+cEtime+"'");
			urls.append("&cEtime="+cEtime);
		}
		if(null!=this.cGsCode && this.cGsCode.length()>0){
			hql.append(" and t.corpGsCode='"+this.cGsCode+"'");
			urls.append("&cGsCode="+this.cGsCode);
		}
		if(null!=this.cProperty && this.cProperty.length()>0){
			hql.append(" and t.corpProperty='"+this.cProperty+"'");
			urls.append("&cProperty="+this.cProperty);
		}
		if(null!=this.cZyb && this.cZyb.length()>0){
			hql.append(" and t.corpIsZyb='"+this.cZyb+"'");
			urls.append("&cZyb="+this.cZyb);
		}
		if(null!=this.cTeshe && this.cTeshe.length()>0){
			hql.append(" and t.corpIsTeshe='"+this.cTeshe+"'");
			urls.append("&cTeshe="+this.cTeshe);
		}
		if(null!=this.cShewei && this.cShewei.length()>0){
			hql.append(" and t.corpIsShewei='"+this.cShewei+"'");
			urls.append("&cShewei="+this.cShewei);
		}
		if(null!=this.cDanger && this.cDanger.length()>0){
			hql.append(" and t.corpIsDanger='"+this.cDanger+"'");
			urls.append("&cDanger="+this.cDanger);
		}
		if(null!=this.cBlzh && this.cBlzh.length()>0){
			hql.append(" and t.corpIsBlzh='"+this.cBlzh+"'");
			urls.append("&cBlzh="+this.cBlzh);
		}
		if(!"4".equals(this.forward)){
			hql.append(" and t.isDele=0");
		}
		hql.append(" and t.corpSize=1");
		this.setUrls(urls.toString());
		return hql.toString();
	}

	public String getCountyId() {
		return countyId;
	}

	public void setCountyId(String countyId) {
		this.countyId = countyId;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getMaxTypeId() {
		return maxTypeId;
	}

	public void setMaxTypeId(String maxTypeId) {
		this.maxTypeId = maxTypeId;
	}

	public String getMinTypeId() {
		return minTypeId;
	}

	public void setMinTypeId(String minTypeId) {
		this.minTypeId = minTypeId;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getCorpStatus() {
		return corpStatus;
	}

	public void setCorpStatus(String corpStatus) {
		this.corpStatus = corpStatus;
	}

	public void setDjpdService(DjpdService djpdService) {
		this.djpdService = djpdService;
	}

}
